import path from "path";
import * as fs from "fs";
import component from "../tool/component";
class ComponentManage {
    componentMap: Map<string, component> = new Map();
    constructor() {

    }
    async init() {
        console.log('ComponentManage init');
        const componentDir = path.resolve(__dirname, '../../component');
        const components = fs.readdirSync(componentDir);
        for (const component of components) {
            const componentModule = await import(`../../component/${component}`);
            this.addComponent(componentModule.default);
        }
    }
    addComponent(component: component) {
        // 使用构造函数名称作为键，如果没有设置name属性
        const componentName = component.name || component.constructor.name;
        this.componentMap.set(componentName, component);
    }
    getComponent(name: string): component {
        const component = this.componentMap.get(name);
        if (!component) {
            debugger;
            throw new Error(`Component with name ${name} not found.`);
        }
        return component;
    }
}
export default new ComponentManage();